/**
 * Created by lx on 2017/9/25.
 */
angular.module('module.table.operation')
    .controller('TableNumScanningController', TableNumScanningController);
TableNumScanningController.$inject = ['$state', 'TableNumService', '$stateParams', '$localStorage'];
function TableNumScanningController($state, TableNumService, $stateParams, $localStorage) {

    // 扫描二维码
    $('.modal-backdrop ').hide(); // 初始化
    var vm = this;

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    vm.goTable = goTable;
    vm.tableid = $stateParams.tableid;
    var shopid = $localStorage[environmental + "shopid"];
    var url = null;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框

    setTimeout(function () {
        activeText();
    }, 200);

    function goTable() {
        $state.go("tabledetail", {tableid: vm.tableid});
    }

    function activeText() {
        url = window.location.href;
        if (ismobile(0) == 1) {
            if (url.indexOf("/scanning") == -1) {
                url = url.replace("/tabledetail", "/scanning");
            }
        }
        var postinfo = {
            "component_appid": wxConfig.component_appid,
            "mer_id": wxConfig.mer_id,
            "path": url
        };
        TableNumService.get_ticket(postinfo).$promise.then(function (wxconfig) { // 调起扫描二维码
            wx.config({
                debug: false,
                appId: "wxd58951ae0414cb55",
                timestamp: wxconfig.timestamp,
                nonceStr: wxconfig.nonceStr,
                signature: wxconfig.signature,
                jsApiList: ['checkJsApi', 'scanQRCode']
            });
            wx.error(function (res) {
                // alert("出错了：" + JSON.stringify(res));
            });

            wx.ready(function () {
                wx.checkJsApi({
                    jsApiList: ['scanQRCode'],
                    success: function (res) {
                    }
                });

                // 扫描二维码
                vm.scanQRCode = function () {
                    wx.scanQRCode({
                        needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                        success: function (res) {
                            var result = res.resultStr;// 当needResult 为 1 时，扫码返回的结果
                            var data = {
                                devid: result,
                                shopid: shopid,
                                tableid: vm.tableid
                            };

                            TableNumService.shopDevRelation(data, function (sccData) {
                                $state.go('tabledetail', {tableid: sccData.tableid});
                            }, function (errData) {
                                vm.messageVisible = true;
                                if (errData.data.message) {
                                    vm.message = errData.data.message + "，请先解绑！";
                                } else {
                                    vm.message = "请扫描有效二维码！";
                                }
                            })
                        }
                    });
                };

            });
            // 微信扫一扫结束
        });
    }

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }
}
